import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import viteCompression from "vite-plugin-compression";
import legacyPlugin from "@vitejs/plugin-legacy";
import AutoImport from "unplugin-auto-import/vite";

const path = require("path");
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  loadEnv(mode, process.cwd());
  return {
    base: "./",
    plugins: [
      vue(),
      vueJsx(),
      viteCompression({
        verbose: true,
        disable: false,
        deleteOriginFile: false,
        threshold: 10240,
        algorithm: "gzip",
        ext: ".gz",
      }),
      AutoImport({
        imports: ["vue", "vue-router"], // 自动导入vue和vue-router相关函数
      }),
      legacyPlugin({
        targets: ["ie >= 11"],
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: {
      hmr: true,
      open: true,
      port: 8087,
      host: "0.0.0.0",
      https: false, //是否打开HTTPs
      proxy: {
        [process.env.VITE_APP_BASE_API]: {
          target: "http://113.44.105.79:8080",
          changeOrigin: true,
          rewrite: (path) => path.replace(process.env.VITE_APP_BASE_API, ""),
        },
      },
    },
    build: {
      target: "modules",
      outDir: "dist",
      // minify:'terser',
      assetsDir: "static", //指定生成静态资源的存放路径
      cssCodeSplit: true, //如果设置为false,整个项目中的所有css会被提取到一个css文件中去
      terserOptions: {
        compress: {
          drop_console: true, //打包时删除console
          drop_debugger: true, //打包时删除debugger
          pure_funcs: ["console.log"],
        },
        output: {
          //去掉注释内容
          comments: true,
        },
      },
    },
  };
});
